package com.netted.studentbook.activity;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.netted.studentbook.R;
import com.netted.studentbook.adapter.ViewPagerAdapter;
import com.netted.studentbook.fragment.CommunityFragMent;
import com.netted.studentbook.fragment.HomeFragMent;
import com.netted.studentbook.fragment.SchoolFragMent;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

public class MainActivity extends FragmentActivity {
    //存放Fragment
    private ArrayList<Fragment> fragmentArrayList;

    //管理Fragment
    private FragmentManager fragmentManager;

    private ViewPager viewPager;//

    private static MediaPlayer mediaPlayer=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.viewPager);

        fragmentManager = getSupportFragmentManager();

        fragmentArrayList = new ArrayList<Fragment>();
        fragmentArrayList.add(new SchoolFragMent());
        fragmentArrayList.add(new HomeFragMent());
        fragmentArrayList.add(new CommunityFragMent());

        fragmentManager = getSupportFragmentManager();

        viewPager.setAdapter(new ViewPagerAdapter(fragmentManager, fragmentArrayList));
    }


    public void teacher(View view){
        playSound(this,R.raw.a1);
        String text = new SimpleDateFormat("HH点mm分ss秒").format(new Date()) + ",赶紧去睡觉了!";
        Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
    }


    private void playSound(Context cxt, int resId){
        MediaPlayer mediaPlayer = MediaPlayer.create(cxt, resId);
        //设置播放起始点
        mediaPlayer.seekTo(0);
        //开始播放
        mediaPlayer.start();

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.stop();
                mediaPlayer = null;
            }
        });
    }
}
